package team.nmsg.ge.system.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 数据库 工具类
 * @author Guodeqi
 *
 */
@Component
public class SysDBHelper {
	
	private static final Logger logger = LoggerFactory.getLogger(SysDBHelper.class);
	
	private static SysDBHelper helper ;
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public SysDBHelper(){
		helper = this;
	}
	
	public static SysDBHelper getHelper(){
		return helper;
	}
	
	
	public Session getCurrentHSession(){
		Session currentSession = sessionFactory.getCurrentSession();
		
		return currentSession;
	}
	
	public Session openHSession(){
		Session openSession = sessionFactory.openSession();
		
		return openSession;
	}
	
	public void closeHSession(  Session sesson ){
		if( sesson!= null && sesson.isOpen() ){
			sesson.close();
		}
	}
	
	
	public static void closeResource(  ResultSet rs , PreparedStatement ps , Connection conn ){
		if( rs != null  ){
			try {
				rs.close();
			} catch (SQLException e) {
				logger.error("" , e);
			}
		}
		if( ps!=null ){
			try {
				ps.close();
			} catch (SQLException e) {
				logger.error("" , e);
			}
		}
	}
	
}
